package com.heaven.back.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.heaven.back.pojo.*;
import com.heaven.back.service.ActionService;
import com.heaven.back.service.RefreshService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;


import java.util.ArrayList;
import java.util.List;



/**
 * The type Action controller.
 */
@Slf4j
@RestController
@CrossOrigin(origins = "*")
public class ActionController {
    @Autowired
    private ActionService actionService;

    @Autowired
    private RefreshService refreshService;

    /**
     * Image get result.
     *
     * @param room the room
     * @return the result
     */
//获取图片
    @GetMapping("/image")
    public Result imageGet(String room) {
        String url = actionService.image(room);
        return Result.success(url);
    }

    /**
     * Back result.
     *
     * @param id       the id
     * @param RoomName the room name
     * @return the result
     * @throws Exception the exception
     */
//back 通过用户id 与返回目的房间进行信息处理
    @PostMapping("/back")
    public Result back(Integer id,
                       String RoomName) throws Exception {
        Room backRoom = Game.getGame().getGameMap().get(RoomName);
        Player p = actionService.searchByid(id);
        String currentRoom = p.getCurrentRoom();
        log.info(String.valueOf(p.getWeighmax()));
        p.set_items((JSONObject.parseArray(p.getItems(), Item.class)));
        //log.info(currentRoom.getLongDescription(Space.game));
        p.setCurrentRoom(backRoom.getDescription());
        String msg = backRoom.getLongDescription(Game.getGame(), p);
        actionService.update(p);

        //通知currentRoom和backRoom的用户更新房间玩家表
        refreshService.updatePlayerList(currentRoom, backRoom.getDescription(), id);

        return Result.success(new BackMsg(msg, p.getCurrentRoom(), backRoom.getItems()));
    }


    /**
     * Take result.
     *
     * @param id   the id
     * @param item the item
     * @return the result
     * @throws Exception the exception
     */
//take
    @PostMapping("/take")
    public Result take(Integer id,
                       Integer item) throws Exception {
        Player p = actionService.searchByid(id);
        List<Item> items = JSONObject.parseArray(p.getItems(), Item.class);
        if (items == null) {
            items = new ArrayList<>();
        }
        p.set_items(items);
        Room currentRoom = Game.getGame().getGameMap().get(p.getCurrentRoom());
        //log.info(currentRoom.getLongDescription(Space.game));
        Item temp = currentRoom.getItems().get(item);
        String itemName = temp.getName();

        if (!p.add_item(temp)) {
            return Result.error("你拿不动" + temp.getName() + "！");
        }
        actionService.update(p);
        currentRoom.removeItem(item);

        refreshService.updateRoomItem(currentRoom.getDescription(), itemName, "拿走了", id);

        return Result.success("你成功拿走了" + temp.getName() + "！");
    }

    /**
     * Drop result.
     *
     * @param id   the id
     * @param item the item
     * @return the result
     * @throws Exception the exception
     */
//drop
    @PostMapping("/drop")
    public Result drop(Integer id,
                       Integer item) throws Exception {

        Player p = actionService.searchByid(id);
        p.set_items((JSONObject.parseArray(p.getItems(), Item.class)));
        Room currentRoom = Game.getGame().getGameMap().get(p.getCurrentRoom());
        Item temp = p.get_items().get(item);
        String itemName = temp.getName();

        System.out.println(temp.getName());

        p.remove_item(item);
        currentRoom.addItem(temp);
        actionService.update(p);

        refreshService.updateRoomItem(currentRoom.getDescription(), itemName, "扔下了", id);

        return Result.success("你成功丢弃了" + temp.getName() + "！");
    }

    /**
     * Eat result.
     *
     * @param id   the id
     * @param item the item
     * @return the result
     * @throws Exception the exception
     */
//eat
    @PostMapping("/eat")
    public Result eat(Integer id,
                      Integer item) throws Exception {
        Player p = actionService.searchByid(id);
        p.set_items((JSONObject.parseArray(p.getItems(), Item.class)));
        Item temp = p.get_items().get(item);
        String msg = actionService.searchEffect(temp.getEatEffect());
        if (!temp.eat(p)) return Result.error(msg);
        p.remove_item(item);
        log.info(String.valueOf(p.getWeighmax()));
        actionService.update(p);

        refreshService.updateRoomItem(p.getCurrentRoom(), temp.getName(), "食用了", id);

        return Result.success(new EatMsg(msg, p));
    }

    /**
     * Go result.
     *
     * @param id        the id
     * @param direction the direction
     * @return the result
     * @throws Exception the exception
     */
//GoCommand 根据id 和方向
    @PostMapping("/go")
    public Result go(Integer id,
                     String direction) throws Exception {
        Player p = actionService.searchByid(id);
        p.set_items((JSONObject.parseArray(p.getItems(), Item.class)));
        Room currentRoom = Game.getGame().getGameMap().get(p.getCurrentRoom());
        Room nextRoom = currentRoom.getExit(direction);
        //log.info(currentRoom.getLongDescription(Space.game));
        if (nextRoom == null) {
            return Result.error("你挠了挠头，差点撞了墙！");
        } else {
            p.setCurrentRoom(nextRoom.getDescription());
            String msg = nextRoom.getLongDescription(Game.getGame(), p);
            actionService.update(p);

            //避免走到传送房间将nextroom更新
            nextRoom = Game.getGame().getGameMap().get(p.getCurrentRoom());

            //通知currentRoom和nextRoom的用户更新房间玩家表
            refreshService.updatePlayerList(currentRoom.getDescription(), nextRoom.getDescription(), id);

            return Result.success(new BackMsg(msg, p.getCurrentRoom(), nextRoom.getItems()));
        }
    }

    /**
     * Backpack result.
     *
     * @param id the id
     * @return the result
     */
//backpack
    @PostMapping("/backpack")
    public Result backpack(Integer id) {
        Player p = actionService.searchByid(id);
        p.set_items((JSONObject.parseArray(p.getItems(), Item.class)));
        ArrayList<Item> _items = JSON.parseObject(p.getItems(), ArrayList.class);
        return Result.success(new BackMsg("当前负重：" + p.getWeight() + " / " + p.getWeighmax(), p.getCurrentRoom(), _items));
    }

    /**
     * Refresh result.
     *
     * @param id the id
     * @return the result
     */
    @PostMapping("/refresh")
    public Result refresh(Integer id) {
        Player p = actionService.searchByid(id);
        p.setIslogin(1);
        actionService.update(p);
        p.setPassword("");
        return Result.success(p);
    }

    /**
     * Currentplayer result.
     *
     * @param id the id
     * @return the result
     */
    @PostMapping("/playerlist")
    public Result currentplayer(Integer id) {
        Player p = actionService.searchByid(id);
        ArrayList<String> namelist = actionService.searchByroom(p.getCurrentRoom());
        return Result.success(namelist);
    }

    /**
     * Player quit result.
     *
     * @param id the id
     * @return the result
     * @throws Exception the exception
     */
    @PostMapping("/quit")
    public Result playerQuit(Integer id) throws Exception {
        Player p = actionService.searchByid(id);
        p.setIslogin(0);
        actionService.update(p);
        return Result.success("成功退出");
    }

    /**
     * Gets all items.
     *
     * @param id the id
     * @return the all items
     */
    @PostMapping("/items")
    public Result getAllItems(Integer id) {
        Player p = actionService.searchByid(id);
        Room currentRoom = Game.getGame().getGameMap().get(p.getCurrentRoom());
        return Result.success(currentRoom.getItems());
    }

}
